#include <stdio.h>
#include <stdlib.h>

int sumArray(int *arr, int size)
{
    int sum = 0;
    int *p = arr;
    int i = 0;
    for(i = 0; i < size; i++)
    {
        sum += *p;
        p++;
    }
    return sum;
}

int main()
{
    int n = 5;
    int i = 0;
    int *arr = (int *)malloc(5 * sizeof(int)); 
    if (arr == NULL) 
    { // 关键：检查内存分配失败的情况
        printf("内存分配失败！\n");
        return 1; // 分配失败，退出程序
    }

    for(i= 0; i < n; i++)
    {
        arr[i] = i + 1;
    }

    int sum = sumArray(arr, n);
    printf("sum = %d\n", sum);

    free(arr);
    arr = NULL;

    return 0;
}
